﻿using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using SD.TeachSystem.Common;
using SD.TeachSystem.Common.LogTools;
using SD.TeachSystem.EntityFrameworkCore;
using SD.TeachSystem.EntityFrameworkCore.Model;
using SD.TeachSystem.IService;
using System.Reflection;

namespace SD.TeachSystem.Service
{
    public class DemoService : IDemoService
    {
        private readonly ModelContext _modelContext;
        public DemoService(ModelContext modelContext)
        {
            _modelContext = modelContext;
        }
        /// <summary>
        /// 新增数据
        /// </summary>
        /// <param name="demo"></param>
        /// <returns></returns>
        public bool AddDemoInfo(Demo demo)
        {
            demo.Id = Guid.NewGuid();
            demo.Name = "SD";
            demo.Remark = "IT公司";
            demo.CreateDate = DateTime.Now;
            demo.CreateUser= "Admin";
            _modelContext.Demo.Add(demo);

            return _modelContext.SaveChanges()>0;
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DelDemoInfo(Guid id)
        {
            var demo = _modelContext.Demo.Find(id);
            if (demo == null)
            {
                return false; // 未找到对应的记录
            }
            _modelContext.Demo.Remove(demo);
            return _modelContext.SaveChanges() > 0; // 返回是否成功
        }
        /// <summary>
        /// 分页查询数据
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public List<Demo> QueryDemoList(int pageIndex, int pageSize)
        {
            return _modelContext.Demo.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); // 分页查询

            //_ = GlobalTools.WriteLog(MethodBase.GetCurrentMethod().Name, "分页查询数据", LogLevel.Info, $"分页后的信息为：【{JsonConvert.SerializeObject(jsonModel)}】");

            //return jsonModel; // 分页查询

        }
        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="demo"></param>
        /// <returns></returns>
        public bool UpdateDemoInfo(Demo demo)
        {
            var existingDemo = _modelContext.Demo.Find(demo.Id);
            if (existingDemo == null)
            {
                return false; // 未找到对应的记录
            }

            existingDemo.Name = "_SD";
            existingDemo.Remark = "_IT公司";

            _modelContext.Demo.Update(existingDemo);
            return _modelContext.SaveChanges() > 0; // 返回是否成功
        }
    }
}
